//
//  ChangePasswordVC.m
//  UDuo
//
//  Created by 铅笔 on 15/11/21.
//  Copyright © 2015年 铅笔. All rights reserved.
//

#import "ChangePasswordVC.h"
//#import "LoginViewController.h"

//导入这个就行了
#import <CommonCrypto/CommonDigest.h>

@interface ChangePasswordVC ()

@property (nonatomic,strong) UIView *view_setOldPassWordBG;//设置原密码背景
@property (nonatomic,strong) UIView *view_setNewPassWordBG;//设置新密码背景
@property (nonatomic,strong) UIView *view_setcheckPassWordBG;//设置新密码背景

@property (nonatomic,strong) UITextField *textField_oldPassword;
@property (nonatomic,strong) UITextField *textField_newPassword;
@property (nonatomic,strong) UITextField *textField_checkPassword;

@property (nonatomic,strong) UIImageView *imageView_oldPassword;
@property (nonatomic,strong) UIImageView *imageView_newPassword;
@property (nonatomic,strong) UIImageView *imageView_checkPassword;

@property (nonatomic,strong) UIButton *button_change;

@property (nonatomic,strong) UILabel *label_tishi;//提示信息

@end

@implementation ChangePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RXDColor(237, 243, 243);
    
    [self initWithAllSubviews];
    self.title = @"修改密码";
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
    //设置登录密码背景
    self.view_setOldPassWordBG = [[UIView alloc] initWithFrame:CGRectMake(43*PROPORTION_WIDTH,64+74*PROPORTION_WIDTH,kScreenWidth - 2*43*PROPORTION_WIDTH, 70*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_setOldPassWordBG];
    self.view_setOldPassWordBG.backgroundColor = RXDColor(253, 253, 253);
    
    self.view_setOldPassWordBG.layer.cornerRadius = 3.0;
    self.view_setOldPassWordBG.clipsToBounds = YES;
    
    self.textField_oldPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, 40)];
    [self.view_setOldPassWordBG addSubview:self.textField_oldPassword];
    self.textField_oldPassword.placeholder = @"请输入原密码";
    self.textField_oldPassword.secureTextEntry = YES;
    self.textField_checkPassword.font = [UIFont systemFontOfSize:Font_SIZE_24];
    self.textField_oldPassword.tag = 15;
    
    [self.textField_oldPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.imageView_oldPassword = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 80, 10,25, 25)];
    [self.view_setOldPassWordBG addSubview:self.imageView_oldPassword];
    self.imageView_oldPassword.image = [UIImage imageNamed:@"login_pwd"];
    
    //设置登录密码背景
    self.view_setNewPassWordBG = [[UIView alloc] initWithFrame:CGRectMake(self.view_setOldPassWordBG.x, self.view_setOldPassWordBG.bottom+40*PROPORTION_WIDTH,self.view_setOldPassWordBG.width,self.view_setOldPassWordBG.height)];
    [self.view addSubview:self.view_setNewPassWordBG];
    self.view_setNewPassWordBG.backgroundColor = RXDColor(253, 253, 253);
    
    self.view_setNewPassWordBG.layer.cornerRadius = 3.0;
    self.view_setNewPassWordBG.clipsToBounds = YES;
    
    self.textField_newPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, 40)];
    [self.view_setNewPassWordBG addSubview:self.textField_newPassword];
    self.textField_newPassword.placeholder = @"请输入新密码";
    self.textField_newPassword.secureTextEntry = YES;
//    self.textField_newPassword.tag = 16;
    
    self.imageView_newPassword = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 80, 10,25, 25)];
    [self.view_setNewPassWordBG addSubview:self.imageView_newPassword];
    self.imageView_newPassword.image = [UIImage imageNamed:@"login_pwd"];
    
    [self.textField_newPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //设置 确认密码背景
    self.view_setcheckPassWordBG = [[UIView alloc] initWithFrame:CGRectMake(self.view_setNewPassWordBG.x, self.view_setNewPassWordBG.bottom+40*PROPORTION_WIDTH,self.view_setNewPassWordBG.width,self.view_setNewPassWordBG.height)];
    [self.view addSubview:self.view_setcheckPassWordBG];
    self.view_setcheckPassWordBG.backgroundColor = RXDColor(253, 253, 253);
    
    self.view_setcheckPassWordBG.layer.cornerRadius = 3.0;
    self.view_setcheckPassWordBG.clipsToBounds = YES;
    
    self.textField_checkPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,kScreenWidth - 100, 40)];
    [self.view_setcheckPassWordBG addSubview:self.textField_checkPassword];
    self.textField_checkPassword.placeholder = @"请确定新密码";
    self.textField_checkPassword.secureTextEntry = YES;
    self.textField_checkPassword.tag = 17;
    
    self.imageView_checkPassword = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 80, 10,25, 25)];
    [self.view_setcheckPassWordBG addSubview:self.imageView_checkPassword];
    self.imageView_checkPassword.image = [UIImage imageNamed:@"login_pwd"];
    
    [self.textField_checkPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //设置修改button
    self.button_change = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view_setcheckPassWordBG.bottom+50*PROPORTION_WIDTH,self.view_setNewPassWordBG.width,92*PROPORTION_WIDTH)];
    [self.view addSubview:self.button_change];
    self.button_change.backgroundColor = RXDColor(192, 201, 205);
    [self.button_change setTitle:@"确认修改" forState:UIControlStateNormal];
    
    [self.button_change addTarget:self action:@selector(changePasswordButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.button_change.layer.cornerRadius = 3.0;
    self.button_change.clipsToBounds = YES;
    
    [self showTishiMessageOrNo];
}

#pragma mark --- 提示信息
- (void) showTishiMessageOrNo
{
    self.label_tishi = [[UILabel alloc] initWithFrame:CGRectMake(self.textField_checkPassword.x+10, self.view_setcheckPassWordBG.height+self.view_setcheckPassWordBG.y+5, self.textField_checkPassword.width, 20)];
    [self.view addSubview:self.label_tishi];
    self.label_tishi.text = @"密码不可以少于6位";
    self.label_tishi.font = [UIFont systemFontOfSize:14];
    self.label_tishi.textColor = [UIColor redColor];
    self.label_tishi.hidden = YES;
}

- (void) textFieldDidChange:(UITextField *)textField
{ 
    if (self.textField_newPassword.text.length>5 && self.textField_oldPassword.text.length>5 && self.textField_checkPassword.text.length>5) {
        self.button_change.backgroundColor = RXDColor(255, 118, 112);
        self.label_tishi.hidden = YES;

    }
    else
    {
        if (self.textField_newPassword.text.length<6) {
            self.label_tishi.hidden = NO;
        }
        else
        {
            self.label_tishi.hidden = YES;
        }
        self.button_change.backgroundColor = RXDColor(192, 201, 205);
    
    }
}

- (void) changePasswordButton
{
    if (self.textField_oldPassword.text==0) {
        [BaseViewController showMessage:@"请输入原密码"];
    }
    if (self.textField_newPassword.text==0) {
        [BaseViewController showMessage:@"请输入新密码"];
    }
    if (self.textField_checkPassword.text==0) {
        [BaseViewController showMessage:@"请确定新密码"];
    }
    if (![self.textField_newPassword.text isEqualToString:self.textField_checkPassword.text]) {
        [BaseViewController showMessage:@"请确定新密码"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"两次密码不一致" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        
        [alertView show];

    }
    else
    {
        if ([self.textField_newPassword.text isEqualToString:self.textField_checkPassword.text]) {
        
        NSDictionary *paras_changePassword = @{@"memberid":[[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"],@"oldpassword":self.textField_oldPassword.text,@"newpassword":self.textField_newPassword.text};
            
            [[HttpRequest shardWebUtil] postNetworkRequestURLString:[NSString stringWithFormat:@"%@member/password/update",MRAPI] parameters:paras_changePassword success:^(id obj) {
                
                NSLog(@"oooooooo%@00%@",obj,obj[@"error"]);
                
                NSString *str_false = obj[@"error"];
                if (str_false.length==0) {
                    [BaseViewController showMessage:@"修改成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                else
                {
                    [BaseViewController showMessage:str_false];
                }
                
            } fail:^(NSError *error) {
                
            }];
      }
    
   }
}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
